using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using _videoConference.BAL;
using System.Data;

namespace _videoConference
{
    public partial class ManageModule : System.Windows.Controls.UserControl, IDisposable
    {
        DataSet ds = new DataSet();
        Label lDllname, lClassname, lVersion, lModulename;
        Button btnEdit, btnDelete, btnCancel;
        Label CaptionMName = new Label();
        Label CaptionDName = new Label();
        Label CaptionCName = new Label();
        Label CaptionVersion = new Label();
        Label CaptionMModule = new Label();
        
        string modid = "";

        IAddPage ia = new IAddPage();
        private bool disposed = false;

        public ManageModule()
        {
            InitializeComponent();
            ModuleInk.EditingMode = InkCanvasEditingMode.None;

            CaptionDName.Foreground = Brushes.White;
            CaptionCName.Foreground = Brushes.White;
            CaptionMName.Foreground = Brushes.White;
            CaptionVersion.Foreground = Brushes.White;
            CaptionMModule.Foreground = Brushes.White;

            CaptionDName.Content = "Dll Name";
            CaptionCName.Content = "Class Name";
            CaptionMName.Content = "Module Name";
            CaptionVersion.Content = "Version";
            CaptionMModule.Content = "Manage Module";

            ModuleInk.Children.Add(CaptionDName);
            ModuleInk.Children.Add(CaptionCName);
            ModuleInk.Children.Add(CaptionMName);
            ModuleInk.Children.Add(CaptionVersion);
            ModuleInk.Children.Add(CaptionMModule);

            CaptionDName.FontWeight = FontWeights.Bold;
            CaptionCName.FontWeight = FontWeights.Bold;
            CaptionMName.FontWeight = FontWeights.Bold;
            CaptionVersion.FontWeight = FontWeights.Bold;
            CaptionMModule.FontWeight = FontWeights.Bold;

            CaptionDName.FontSize = 12.0;
            CaptionCName.FontSize = 12.0;
            CaptionMName.FontSize = 12.0;
            CaptionVersion.FontSize = 12.0;
            CaptionMModule.FontSize = 12.0;

            CaptionDName.SetValue(InkCanvas.TopProperty, 10.0);
            CaptionCName.SetValue(InkCanvas.TopProperty, 10.0);
            CaptionMName.SetValue(InkCanvas.TopProperty, 10.0);
            CaptionVersion.SetValue(InkCanvas.TopProperty, 10.0);
            CaptionMModule.SetValue(InkCanvas.TopProperty, 10.0);

            CaptionDName.SetValue(InkCanvas.LeftProperty, 10.0);
            CaptionCName.SetValue(InkCanvas.LeftProperty, 280.0);
            CaptionMName.SetValue(InkCanvas.LeftProperty, 400.0);
            CaptionVersion.SetValue(InkCanvas.LeftProperty, 550.0);
            CaptionMModule.SetValue(InkCanvas.LeftProperty, 640.0);

            LinearGradientBrush l;
            GradientStop gs1;
            GradientStop gs2;

            l = new LinearGradientBrush();

            Point p1 = new Point(0.0, 0.5);
            Point p2 = new Point(1.0, 0.5);

            l.StartPoint = p1;
            l.EndPoint = p2;

            gs2 = new GradientStop(System.Windows.Media.Color.FromRgb(89, 89, 89), 0.11428571428571427);
            gs1 = new GradientStop(System.Windows.Media.Color.FromRgb(115, 115, 115), 0.51428571428571427);

            System.Windows.Media.RotateTransform RT;
            RT = new RotateTransform(90.0);

            l.Transform = RT;
            l.GradientStops.Add(gs1);
            l.GradientStops.Add(gs2);

            ds = ia.getmoduleinfo();

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    lDllname = new Label();
                    lDllname.Foreground = Brushes.White;
                    lClassname = new Label();
                    lClassname.Foreground = Brushes.White;
                    lModulename = new Label();
                    lModulename.Foreground = Brushes.White;  
                    lVersion=new Label();
                    lVersion.Foreground = Brushes.White;

                    btnEdit = new Button();
                    btnDelete = new Button();

                    btnEdit.Click += new RoutedEventHandler(btnEdit_Click);
                    btnDelete.Click += new RoutedEventHandler(btnDelete_Click);

                    btnDelete.Content="Delete";
                    btnEdit.Content = "Edit";

                    btnEdit.Width = 60.0;
                    btnDelete.Width = 80.0;

                    btnDelete.Background = l;
                    btnDelete.Foreground = Brushes.White;

                    btnEdit.Background = l;
                    btnEdit.Foreground = Brushes.White;

                    lDllname.Content = ds.Tables[0].Rows[i][0].ToString();
                    lDllname.Tag = ds.Tables[0].Rows[i][6].ToString();
                    lDllname.FontSize = 14.0;

                    lClassname.Content = ds.Tables[0].Rows[i][1].ToString();
                    lClassname.Tag = ds.Tables[0].Rows[i][6].ToString();
                    lClassname.FontSize = 14.0;

                    lModulename.Content = ds.Tables[0].Rows[i][2].ToString();
                    lModulename.Tag = ds.Tables[0].Rows[i][6].ToString();
                    lModulename.FontSize = 14.0;

                    lVersion.Content = ds.Tables[0].Rows[i][4].ToString();
                    lVersion.Tag = ds.Tables[0].Rows[i][6].ToString();
                    lVersion.FontSize = 14.0;

                    btnEdit.Tag = ds.Tables[0].Rows[i][6].ToString();
                    btnDelete.Tag = ds.Tables[0].Rows[i][6].ToString();

                    lDllname.SetValue(InkCanvas.LeftProperty, 10.0);
                    lClassname.SetValue(InkCanvas.LeftProperty, 280.0);
                    lModulename.SetValue(InkCanvas.LeftProperty, 400.0);
                    lVersion.SetValue(InkCanvas.LeftProperty, 550.0);
                    btnEdit.SetValue(InkCanvas.LeftProperty, 640.0);
                    btnDelete.SetValue(InkCanvas.LeftProperty, 720.0);

                    lDllname.SetValue(InkCanvas.TopProperty, (i + 1) * 31.0);
                    lClassname.SetValue(InkCanvas.TopProperty, (i + 1) * 31.0);
                    lModulename.SetValue(InkCanvas.TopProperty, (i + 1) * 31.0);
                    lVersion.SetValue(InkCanvas.TopProperty, (i + 1) * 31.0);
                    btnEdit.SetValue(InkCanvas.TopProperty, (i + 1) * 31.0);
                    btnDelete.SetValue(InkCanvas.TopProperty, (i + 1) * 31.0);

                    ModuleInk.Children.Add(lDllname);
                    ModuleInk.Children.Add(lClassname);
                    ModuleInk.Children.Add(lModulename);
                    ModuleInk.Children.Add(lVersion);
                    ModuleInk.Children.Add(btnEdit);
                    ModuleInk.Children.Add(btnDelete);
                }
            }

            btnCancel = new Button();
            btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
            btnCancel.Content = "Cancel";
            btnCancel.Width = 80.0;
            btnCancel.Height = 25.0;
            btnCancel.SetValue(InkCanvas.LeftProperty, 280.0);
            btnCancel.SetValue(InkCanvas.TopProperty, double.Parse(((Button)ModuleInk.Children[ModuleInk.Children.Count - 1]).GetValue(InkCanvas.TopProperty).ToString()) + 40.0);
            ModuleInk.Children.Add(btnCancel);          
            
            btnCancel.Background = l;
            btnCancel.Foreground = Brushes.White;         
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            ((Canvas)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("cnvMain")).IsEnabled = true;
            ((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).IsEnabled = true;
            ((InkCanvas)this.Parent).Children.Remove(this);
        }

        void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult r = MessageBox.Show("Are you sure that you want to delete the module entry?", "Module Delete", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No, MessageBoxOptions.DefaultDesktopOnly);
            try
            {
                if (r == MessageBoxResult.Yes)
                {                    
                    for (int i = 0; i < ModuleInk.Children.Count; i++)
                    {
                        if (ModuleInk.Children[i].GetValue(InkCanvas.LeftProperty).ToString() == "400" && ModuleInk.Children[i].GetValue(InkCanvas.TopProperty).ToString() != "10")
                        {
                            if (((Label)ModuleInk.Children[i]).Tag.ToString() == ((Button)sender).Tag.ToString())
                            {
                                modid = ((Button)sender).Tag.ToString();

                                bool bool1 = ia.delmoduleinfo(modid);                                
                                if (bool1)
                                {
                                    int j = 0;
                                    while (j < ModuleInk.Children.Count)
                                    {
                                        try
                                        {
                                            if (ModuleInk.Children[j].GetType() == typeof(Label) && ((Label)ModuleInk.Children[j]).Tag != null && ((Label)ModuleInk.Children[j]).Tag.ToString() == ((Button)sender).Tag.ToString())
                                            {
                                                ModuleInk.Children.Remove(ModuleInk.Children[j]);
                                            }
                                            else if (ModuleInk.Children[j].GetType() == typeof(Button) && ((Button)ModuleInk.Children[j]).Tag != null && ((Button)ModuleInk.Children[j]).Tag.ToString() == ((Button)sender).Tag.ToString())
                                            {
                                                ModuleInk.Children.Remove(ModuleInk.Children[j]);
                                            }
                                            else
                                            {
                                                j++;
                                            }
                                        }
                                        catch (Exception exp)
                                        {
                                            //MessageBox.Show(exp.Message);
                                            exp.Data.Add("My Key", "1videoConference--:--ManageMeeting.xaml.cs--:--btnDelete_Click()--:--");
                                            clsUtility.LogException = exp;
                                            break;
                                        }
                                    }
                                }
                                ((Canvas)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("cnvMain")).IsEnabled = true;
                                ((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).IsEnabled = true;
                                ((InkCanvas)this.Parent).Children.Remove(this);
                            }
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show(exp.Message);

            }
            
        }

        void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            AddModuleControl amc = new AddModuleControl();
            for (int i = 0; i < ModuleInk.Children.Count; i++)
            {
                if (ModuleInk.Children[i].GetValue(InkCanvas.LeftProperty).ToString() == "400" && ModuleInk.Children[i].GetValue(InkCanvas.TopProperty).ToString() != "10")
                {
                    if (((Label)ModuleInk.Children[i]).Tag.ToString() == ((Button)sender).Tag.ToString())
                    {
                        UserInfo.moduleid = ((Button)sender).Tag.ToString();
                    }
                }
            }
            DataSet dsnew = ia.editmodule(UserInfo.moduleid);

           if (dsnew.Tables.Count > 0 && dsnew.Tables[0].Rows.Count > 0)
           {
               for (int j = 0; j < dsnew.Tables[0].Rows.Count; j++)
               {
                   amc.lbDllName.IsEnabled = false;
                   amc.lbDllName.Text = dsnew.Tables[0].Rows[j][0].ToString();                  
               }
           }
            ((InkCanvas)this.Parent).Children.Add(amc);
            ((InkCanvas)this.Parent).Children.Remove(this);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {

                }
                disposed = true;

            }
        }

        ~ManageModule()
        {
            Dispose(false);
        }
    }
}